//
//  CXRetweetView.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/22.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit
import SnapKit
import YYText

class CXRetweetView: UIView {
    
    /// 记录自身view的底部约束
    var bottomCons: Constraint?
    
    var statusViewModel: CXStatusViewModel? {
        didSet {
//            contentView.text = statusViewModel?.retweetText
            
            if let attrStr = statusViewModel?.retwetAttrString {
                contentView.attributedText = attrStr
                let maxSize = CGSize(width: SCREENW - 2 * CGFloat(CXComponentsInMargin), height: CGFloat(MAXFLOAT))
                let layout = YYTextLayout(containerSize: maxSize, text: attrStr)
                contentView.snp_updateConstraints(closure: { (make) -> Void in
                    make.height.equalTo(layout.textBoundingSize.height)
                })
            }
            
            //图片区域的判断实现逻辑与之前cell里面的类似
            bottomCons?.uninstall()
            if let pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls where pic_urls.count > 0 {
                photoView.hidden = false
                photoView.pic_urls = pic_urls
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(photoView).offset(CXComponentsInMargin).constraint
                })
            } else {
                photoView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(contentView).offset(CXComponentsInMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 内容
    private lazy var contentView: YYLabel = {
        let label = YYLabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(CXStatusFontSize)
        label.numberOfLines = 0
        return label
    }()
    //懒加载图片显示view
    private lazy var photoView: CXStatusPhotoView = {
        let photoV = CXStatusPhotoView()   //可以这么去用，只要内部实现了那个必须实现的构造函数
        photoV.backgroundColor = self.backgroundColor
        return photoV
    }()
    
}

extension CXRetweetView {
    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 242.0 / 255, alpha: 1)
        addSubview(contentView)
        addSubview(photoView)
 /*
        UILabel:0x7fc07323a680.top == CXRetweetView:0x7fc07323a500.top + 8.0>",
        CXRetweetView:0x7fc07323a500.bottom == UILabel:0x7fc07323a680.bottom + 8.0>",,
        
*/
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(CXComponentsInMargin)
            make.top.equalTo(self).offset(CXComponentsInMargin)
            make.trailing.equalTo(self).offset(-CXComponentsInMargin)
        }
        photoView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView.snp_bottom).offset(CXComponentsInMargin)


            //size由photoView内部实现的size决定
        }
        
        //约束self的自身底部约束
        self.snp_makeConstraints { (make) -> Void in
            bottomCons = make.bottom.equalTo(photoView).offset(CXComponentsInMargin).constraint
        }
        
        //开启高亮效果，获取高亮效果的内容,高亮效果在ViewModel中已经设置了
        contentView.highlightTapAction = { (containerView, text, range, rect) -> Void in
            
            let res = text.attribute(YYTextHighlightAttributeName, atIndex: range.location, effectiveRange: nil) as! YYTextHighlight
            //通过之前在ViewModel设置的 userInfo 来获取并打印高亮内容
            print(res.userInfo["content"])
        }
    }
}
